package com.cskaoyan.service.wx.llk;

import com.cskaoyan.bean.wx.llk.*;
import com.cskaoyan.mapper.pz.ConfigMapper;
import com.cskaoyan.mapper.wx.llk.AddressMapper1;
import com.cskaoyan.mapper.wx.llk.WxCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class WxCartServiceImpl implements WxCartService {

    @Autowired
    WxCartMapper wxCartMapper;

    @Autowired
    AddressMapper1 addressMapper;

    @Autowired
    ConfigMapper configMapper;


    @Override
    public WxItemsBean<WxCart> queryIndex(int id) {
        //用cartExample 查询出 该用户的购物车信息
        WxCartExample cartExample = new WxCartExample();
        WxCartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andUserIdEqualTo(id)
                .andDeletedEqualTo(false);
        List<WxCart> carts = wxCartMapper.selectByExample(cartExample);

        //遍历查询到的信息 得到他们的商品数量 和 总价
        Integer checkedGoodsCount = 0;
        Double checkedGoodsAmount = 0.0;
        Integer goodsCount = 0;
        Double goodsAmount = 0.0;
        for (WxCart cart : carts) {
            goodsCount = goodsCount + cart.getNumber();
            goodsAmount = goodsAmount + cart.getPrice().doubleValue() * cart.getNumber();
            if (cart.getChecked() == true){
                checkedGoodsCount = checkedGoodsCount + cart.getNumber();
                checkedGoodsAmount = checkedGoodsAmount + cart.getPrice().doubleValue() * cart.getNumber();
            }
        }

        CartTotalBean cartTotalBean = new CartTotalBean(goodsCount, checkedGoodsCount.doubleValue(),
                goodsAmount.doubleValue(), checkedGoodsAmount);

        return WxItemsBean.data(cartTotalBean,carts);
    }

    @Override
    public WxItemsBean<WxCart> updateChecked(CheckedBO checkedBO, int id) {
        WxCartExample cartExample = new WxCartExample();
        //要修改的条件
        WxCartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andProductIdIn(checkedBO.getProductIds())
                .andUserIdEqualTo(id)
                .andDeletedEqualTo(false);

        //要修改的数据
        WxCart wxCart = new WxCart();
        wxCart.setChecked(checkedBO.getIsChecked());

        wxCartMapper.updateByExampleSelective(wxCart,cartExample);
        return queryIndex(id);
    }

    @Override
    public void WxCartupdate(WxCartUpdateBO wxCartUpdateBO) {
        WxCartExample cartExample = new WxCartExample();
        WxCartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andIdEqualTo(wxCartUpdateBO.getId())
                .andDeletedEqualTo(false)
                .andGoodsIdEqualTo(wxCartUpdateBO.getGoodsId())
                .andProductIdEqualTo(wxCartUpdateBO.getProductId());

        WxCart wxCart = new WxCart();
        wxCart.setNumber(wxCartUpdateBO.getNumber());

        wxCartMapper.updateByExampleSelective(wxCart,cartExample);
    }

    @Override
    public void addCart(WxCartAddBO wxCartAddBO, Integer userId) {
        //添加购物车之前 先判断该用户 是否已经加购过了  加购过则增加数量
        int affectedRows= wxCartMapper.updateCartGoods(wxCartAddBO,userId);
        if (affectedRows == 0){
            //没有加购过 添加
            WxCart wxCart = wxCartMapper.selectGoodsByGoodsId(wxCartAddBO.getGoodsId());
            wxCart.setUserId(userId);
            wxCart.setGoodsId(wxCartAddBO.getGoodsId());
            wxCart.setNumber(wxCartAddBO.getNumber());
            wxCart.setProductId(wxCartAddBO.getProductId());
            wxCart.setAddTime(new Date());
            wxCart.setUpdateTime(new Date());
            wxCartMapper.insertSelective(wxCart);
        }

    }

    @Override
    public Integer WxCartFastAdd(WxCartAddBO wxCartAddBO, Integer userId) {
        addCart(wxCartAddBO,userId);
        //因为是刚插入的 所以查询刚添加的cartId
        Integer cartId = wxCartMapper.selectMaxCartId();
        return cartId;
    }

    @Override
    public Integer WxCartGoodsCount(Integer userId) {

        Integer count = wxCartMapper.selectCountByUserId(userId);
        return count;
    }

    @Override
    public WxItemsBean<WxCart> WxCartDelete(Map<String,Integer[]> map, Integer userId) {

        //将map中的Integer[] 转为List<Integer>  好用于后续的查询条件
        List<Integer> integerList = Arrays.asList(map.get("productIds"));

        WxCartExample wxCartExample = new WxCartExample();
        WxCartExample.Criteria criteria = wxCartExample.createCriteria();
        criteria.andProductIdIn(integerList);

        WxCart wxCart = new WxCart();
        wxCart.setDeleted(true);
        wxCartMapper.updateByExampleSelective(wxCart,wxCartExample);
        //再查询出修改后的购物车信息用于返回
        WxItemsBean<WxCart> data = queryIndex(userId);
        return data;
    }

    @Override
    public CartCheckoutVO WxCartCheckout(Integer cartId, Integer addressId, Integer couponId, Integer grouponRulesId, Integer userId) {

        //查询地址
        Address checkedAddress = addressMapper.selectByPrimaryKey(addressId);

        //查询要下单的 购物车中的商品
        WxCartExample wxCartExample = new WxCartExample();
        WxCartExample.Criteria criteria = wxCartExample.createCriteria();
        criteria.andUserIdEqualTo(userId)
                .andCheckedEqualTo(true)
                .andDeletedEqualTo(false);
        if (cartId != 0){
            criteria.andIdEqualTo(cartId);
        }
        List<WxCart> checkedGoodsList = wxCartMapper.selectByExample(wxCartExample);
        Double goodsTotalPrice = 0.0;
        for (WxCart wxCart : checkedGoodsList) {
            goodsTotalPrice = goodsTotalPrice + wxCart.getPrice().doubleValue();
        }

        //查询该用户可用优惠卷数量  根据 商品价格和用户id 来查询
        Integer num = wxCartMapper.selectAvailableCouponLength(goodsTotalPrice,userId);

        //查询付运费的要求
        Integer freightValue = Integer.valueOf(selectKeyValue("cskaoyan_mall_express_freight_value"));
        Integer freightPrice = 0;
        //商品总价小于最低要求 就需要付运费
        if (goodsTotalPrice < freightValue){
            freightPrice = Integer.valueOf(selectKeyValue("cskaoyan_mall_express_freight_min"));
        }

        //根据优惠卷id 查出折扣
        Double couponPrice = 0.0;
        if (couponId != 0){
            couponPrice = Double.valueOf(wxCartMapper.selectCouponPrice(couponId));
        }

        //订单价格
        Double orderTotalPrice = goodsTotalPrice + freightPrice - couponPrice;

        //实付价格  应该减去 用户积分的 但是没有找到用户积分在哪里
        Double actualPrice = orderTotalPrice;

        //将要返回的数据封装
        CartCheckoutVO cartCheckoutVO = new CartCheckoutVO(0,0,
                checkedAddress,
                actualPrice,
                orderTotalPrice,
                couponPrice,
                num,
                couponId,
                freightPrice,
                goodsTotalPrice,
                addressId,
                checkedGoodsList);

        return cartCheckoutVO;
    }

    private String selectKeyValue(String keyName){
        String keyValue = configMapper.selectKeyName(keyName);
        return keyValue;
    }


}
